package com.platform.api.bzproject.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.platform.api.bzproject.model.TzCollectionInfo;
import com.platform.api.bzproject.service.ITzCollectionInfoService;
import com.platform.common.core.Result;
import com.platform.security.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户收款信息 前端控制器
 * </p>
 *
 * @author guoshao
 * @since 2022-01-01
 * 源码联系微信：2549789059
 */
@RestController
@RequestMapping("u/collection")
public class TzCollectionInfoController {

    @Autowired
    private ITzCollectionInfoService iTzCollectionInfoService;


    @PostMapping("addInfo")
    public Result addInfo(@RequestBody TzCollectionInfo collectionInfo){
        try {

            String userId = SecurityUtils.getUser().getUserId();
            collectionInfo.setUserId(userId);
            collectionInfo.setCreateTime(new Date());
            iTzCollectionInfoService.save(collectionInfo);
            return Result.success();
        }catch (Exception e){
            System.out.println(e.getMessage());
            return Result.failure("添加失败");
        }
    }

    @PostMapping("selectList")
    public Result selectList(){
        try {
            String userId = SecurityUtils.getUser().getUserId();
            List<TzCollectionInfo> list = iTzCollectionInfoService.list(
                    new QueryWrapper<TzCollectionInfo>().eq("user_id", userId).
                            eq("del_flag",0).orderByDesc("is_default").orderByDesc("create_time"));

            list.forEach(collectionInfo -> {
                String account = collectionInfo.getAccount();
                if(account.length() >= 4){
                    String accountr = account.substring(0, 4) + "********" + account.substring(account.length() - 4, account.length());
                    collectionInfo.setAccount(accountr);
                }

                String resMobile = collectionInfo.getResMobile();
                String phone = resMobile.substring(0, 3) + "****" + resMobile.substring(resMobile.length() - 4, resMobile.length());
                collectionInfo.setResMobile(phone);

                String idCard = collectionInfo.getIdCard();
                if(idCard.length() >= 4){
                    String idCardNo = idCard.substring(0, 4) + "********" + idCard.substring(idCard.length() - 4, idCard.length());
                    collectionInfo.setIdCard(idCardNo);
                }


            });
            return Result.success(list);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return Result.failure("加载失败");
        }
    }

    @GetMapping("clickDefault")
    public Result clickDefault(Integer id){
        try {
            String userId = SecurityUtils.getUser().getUserId();
            TzCollectionInfo collectionInfo = new TzCollectionInfo();
            collectionInfo.setUserId(userId);
            iTzCollectionInfoService.updateDefault(collectionInfo);

            collectionInfo = iTzCollectionInfoService.getById(id);
            collectionInfo.setIsDefault(1);
            iTzCollectionInfoService.updateById(collectionInfo);
            return Result.success();
        }catch (Exception e){
            System.out.println(e.getMessage());
            return Result.failure("设置失败");
        }
    }

    @GetMapping("clickDelete")
    public Result clickDelete(Integer id){
        try {
            TzCollectionInfo collectionInfo = iTzCollectionInfoService.getById(id);
            collectionInfo.setIsDefault(0);
            collectionInfo.setDelFlag(1);
            iTzCollectionInfoService.updateById(collectionInfo);
            return Result.success();
        }catch (Exception e){
            System.out.println(e.getMessage());
            return Result.failure("删除失败");
        }
    }

    @GetMapping("defaultData")
    public Result defaultData(){
        try {
            String userId = SecurityUtils.getUser().getUserId();
            TzCollectionInfo collectionInfo = iTzCollectionInfoService.getOne(new QueryWrapper<TzCollectionInfo>()
                    .eq("user_id", userId).eq("is_default", 1)
                    .eq("del_flag", 0));

            if(null != collectionInfo){
                String account = collectionInfo.getAccount();
                if(account.length() >= 4){
                    String accountr = account.substring(0, 4) + "********" + account.substring(account.length() - 4, account.length());
                    collectionInfo.setAccount(accountr);
                }

                String resMobile = collectionInfo.getResMobile();
                String phone = resMobile.substring(0, 3) + "****" + resMobile.substring(resMobile.length() - 4, resMobile.length());
                collectionInfo.setResMobile(phone);

                String idCard = collectionInfo.getIdCard();
                if(idCard.length() >= 4){
                    String idCardNo = idCard.substring(0, 4) + "********" + idCard.substring(idCard.length() - 4, idCard.length());
                    collectionInfo.setIdCard(idCardNo);
                }
            }

            return Result.success(collectionInfo);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return Result.failure("获取失败");
        }
    }


}
